﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Orbitone.MeetingRooms.Services;
using Orbitone.MeetingRooms.Services.Model;

namespace Orbitone.MeetingRooms.RoomFinder
{
    public partial class Calendar : Page
    {
        #region Exchange

        public string starttime
        {
            get { return ConfigurationManager.AppSettings["starttime"]; }
        }

        public string endtime
        {
            get { return ConfigurationManager.AppSettings["endtime"]; }
        }

        private IExchangeService _exchangeService;

        private LdapService _ldapService;
        private List<MailboxModel> _rooms;

        public LdapService LdapService
        {
            get { return _ldapService = _ldapService ?? new LdapService(); }
        }

        public IExchangeService ExchangeService
        {
            get { return _exchangeService = _exchangeService ?? new ExchangeService(); }
        }

        public List<MailboxModel> Rooms
        {
            get { return _rooms = _rooms ?? LdapService.FindRooms(); }
        }

        public DateTime StartDate
        {
            get
            {
                return DateTime.ParseExact(_selectedDate.ToString("yyyyMMdd") + "T" + starttime, "yyyyMMddTHH:mm:ss",
                                           null);
                ;
            }
        }

        public DateTime EndDate
        {
            get
            {
                return DateTime.ParseExact(_selectedDate.AddDays(1).ToString("yyyyMMdd") + "T" + endtime,
                                           "yyyyMMddTHH:mm:ss", null);
                ;
            }
        }

        public List<AvailabilityModel> Availability
        {
            get { return ExchangeService.GetAvailability(Rooms, StartDate, EndDate, 30); }
        }

        #endregion

        private DateTime _selectedDate;

        public DateTime SelectedDate
        {
            get
            {
                _selectedDate = DateTime.Now;
                string date = Request.QueryString["date"];
                if (!string.IsNullOrEmpty(date))
                {
                    _selectedDate = DateTime.ParseExact(date, "ddMMyyyy", null);
                }

                if (_selectedDate == DateTime.MinValue)
                {
                    _selectedDate = DateTime.Now;
                }

                return _selectedDate;
            }
        }

        public string EventId { get; set; }
        public string EventMaibox { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            DateBrowser.SelectedDate = SelectedDate;

            CalendarGrid.CalendarItemClick = CalendarItemClick;
            CalendarGrid.Availability = Availability;
            CalendarGrid.StartDate = StartDate;
            CalendarGrid.EndDate = EndDate;

            CreateCalendarItem.Rooms = Rooms;
            CreateCalendarItem.ExchangeService = ExchangeService;
            CreateCalendarItem.SelectedDate = SelectedDate;

            UpdateCalendarItem.Rooms = Rooms;
            UpdateCalendarItem.ExchangeService = ExchangeService;

            

            HttpCookie httpCookie = Request.Cookies[Globals.COOKIE_NAME];
            if (httpCookie != null)
            {
                CreateCalendarItem.LoginId = httpCookie["id"];
                CreateCalendarItem.LoginUsername = httpCookie["name"];
                UpdateCalendarItem.LoginUsername = httpCookie["name"];
                CalendarGrid.UserId = httpCookie["id"];
            }
        }

        private void CalendarItemClick(object sender, EventArgs e)
        {
            var linkButton = (LinkButton) sender;
            EventId = linkButton.CommandArgument;
            EventMaibox = linkButton.CommandName;

            UpdateCalendarItem.EventId = EventId;
            UpdateCalendarItem.EventMailbox = EventMaibox;

            var StartDate = new DateTime();
            var EndDate = new DateTime();
            foreach (var CalendarEvent in Availability.SelectMany(x => x.CalendarEvents))
            {
                
                if (UpdateCalendarItem.EventId == CalendarEvent.Id)
                {
                    StartDate = CalendarEvent.StartDate;
                    EndDate = CalendarEvent.EndDate;
                    break;
                }
                
            }
            
            
            UpdateCalendarItem.Update(StartDate, EndDate);
        }
    }
}